package com.LCQ.crm.workbench.controller;

import com.LCQ.crm.exception.ClueConvertException;
import com.LCQ.crm.settings.domain.User;
import com.LCQ.crm.utils.DateTimeUtil;
import com.LCQ.crm.utils.PrintJson;
import com.LCQ.crm.utils.ServiceFactory;
import com.LCQ.crm.utils.UUIDUtil;
import com.LCQ.crm.vo.PaginationVo;
import com.LCQ.crm.workbench.domain.Activity;
import com.LCQ.crm.workbench.domain.Clue;
import com.LCQ.crm.workbench.domain.Tran;
import com.LCQ.crm.workbench.service.ClueService;
import com.LCQ.crm.workbench.service.impl.ClueServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClueServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("!!!!ClueService!!!!");
        String path = request.getServletPath();

        if(path==null){
            return;
        }

        if(path.contains("save.do")){
            save(request,response);
            return;
        }else if(path.contains("getPageList.do")){
            getPageList(request,response);
            return;
        }else if(path.contains("detail.do")){
            detail(request,response);
            return;
        }else if(path.contains("getAssociatedAct.do")){
            getAssociatedAct(request,response);
            return;
        }else if(path.contains("unbindActCLue.do")){
            unbindActCLue(request,response);
            return;
        }else if(path.contains("getUnbindingActByCond.do")){
            getUnbindingActByCond(request,response);
            return;
        }else if(path.contains("bindActClue.do")){
            bindActClue(request,response);
            return;
        }else if(path.contains("getBindActByCond.do")){
            getBindActByCond(request,response);
            return;
        }else if(path.contains("convert.do")){
            convert(request,response);
            return;
        }

    }

    private void convert(HttpServletRequest request, HttpServletResponse response) {
        String clueId = request.getParameter("clueId");
        boolean isCTrans = Boolean.valueOf(request.getParameter("isCTrans"));
        Tran tran = null;
        String createBy = ((User)(request.getSession().getAttribute("user"))).getName();
        if(isCTrans){
            tran = new Tran();
            tran.setMoney(request.getParameter("tMoney"));
            tran.setExpectedDate(request.getParameter("tExpectedDate"));
            tran.setStage(request.getParameter("tStage"));
            tran.setActivityId(request.getParameter("tActivityId"));
            tran.setName(request.getParameter("tName"));
            tran.setCreateBy(createBy);
        }
         ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        try {
            clueService.convert(clueId,tran,createBy);
            PrintJson.printJsonFlag(response,true);
        } catch (ClueConvertException e) {
            e.printStackTrace();
            Map<String,Object> resMap = new HashMap<>();
            resMap.put("success",false);
            resMap.put("msg",e.getMessage());
            PrintJson.printJsonObj(response,resMap);
        }
    }

    private void getBindActByCond(HttpServletRequest request, HttpServletResponse response) {
        String clueId = request.getParameter("id");
        String ActName = request.getParameter("name");
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        Map<String,String> map = new HashMap<>();
        map.put("clueId",clueId);
        map.put("actName",ActName);
        List<Activity> list = clueService.getBindActByCond(map);
        PrintJson.printJsonObj(response,list);
    }

    private void bindActClue(HttpServletRequest request, HttpServletResponse response) {
        String clueId = request.getParameter("clueId");
        String[] actIds = request.getParameterValues("actId");
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        boolean result = clueService.bindActClue(clueId,actIds);
        if (result){
            PrintJson.printJsonFlag(response,true);
        }else {
            PrintJson.printJsonFlag(response,false);
        }
    }

    private void getUnbindingActByCond(HttpServletRequest request, HttpServletResponse response) {
        String clueId = request.getParameter("id");
        String actName = request.getParameter("name");
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        Map<String,String> map = new HashMap();
        map.put("clueId",clueId);
        map.put("actName",actName);
        PaginationVo page = clueService.getUnbindingActByCond(map);
        PrintJson.printJsonObj(response,page);
    }

    private void unbindActCLue(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        boolean result = clueService.unbindActCLue(id);
        if(result){
            PrintJson.printJsonFlag(response,true);
        }else{
            PrintJson.printJsonFlag(response,false);
        }
    }

    private void getAssociatedAct(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        List<Map> listMap = clueService.getAssociatedAct(id);
        PrintJson.printJsonObj(response,listMap);
    }

    private void detail(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        Clue clue = clueService.getClueById(id);
        request.setAttribute("clue",clue);
        try {
            request.getRequestDispatcher("/workbench/clue/detail.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getPageList(HttpServletRequest request, HttpServletResponse response) {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        Map<String,Object> map = new HashMap();
        map.put("company",request.getParameter("company"));
        map.put("fullname",request.getParameter("fullname"));
        map.put("owner",request.getParameter("owner"));
        map.put("phone",request.getParameter("phone"));
        map.put("mphone",request.getParameter("mphone"));
        map.put("state",request.getParameter("state"));
        map.put("source",request.getParameter("source"));
        map.put("index",(pageNum-1)*pageSize);
        map.put("pageSize",pageSize);
        PaginationVo<Clue> page = clueService.getPageList(map);
        PrintJson.printJsonObj(response,page);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) {
        Clue clue = new Clue();
        clue.setCompany(request.getParameter("company"));
        clue.setAppellation(request.getParameter("appellation"));
        clue.setAddress(request.getParameter("address"));
        clue.setDescription(request.getParameter("description"));
        clue.setContactSummary(request.getParameter("contactSummary"));
        clue.setEmail(request.getParameter("email"));
        clue.setFullname(request.getParameter("fullname"));
        clue.setOwner(request.getParameter("owner"));
        clue.setJob(request.getParameter("job"));
        clue.setPhone(request.getParameter("phone"));
        clue.setWebsite(request.getParameter("website"));
        clue.setMphone(request.getParameter("mphone"));
        clue.setState(request.getParameter("state"));
        clue.setSource(request.getParameter("source"));
        clue.setId(UUIDUtil.getUUID());
        clue.setCreateBy(((User)request.getSession().getAttribute("user")).getName());
        clue.setCreateTime(DateTimeUtil.getSysTime());
        clue.setNextContactTime(request.getParameter("nextContactTime"));
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        boolean result = clueService.save(clue);
        if(result){
            PrintJson.printJsonFlag(response,true);
        }else{
            PrintJson.printJsonFlag(response,false);
        }
    }

}
